package com.ictravi.iamunit;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * Class to allow to add multiple failures to a testcase
 * 
 * @author Albert Louw (alouw@ictavi.com)
 * @version v1.0.0
 * @since v1.0.0
 **/
public class Failures {
	private List<String> failures = new LinkedList<String>();

	/**
	 * add a failure to the list with a message
	 * @param message
	 * 			String describing the failure
	 * @since v1.0.0
	 */
	public void add(String message) {
		failures.add(message);
	}

	/**
	 * add a failure to the list with a message
	 * @param message
	 * 			String describing the failure
	 * @param expected
	 * 			collection of expected values
	 * @param actual
	 * 			collection of actual values
	 * @since v1.0.0
	 */
	@SuppressWarnings("rawtypes")
	public void add(String message, Collection expected, Collection actual) {
		String formatted = "";
		if (message != null)
			formatted = message + " ";
		failures.add(formatted + "expected:<" + expected + "> but was:<"
				+ actual + ">");
	}

	/**
	 * get the current value for failures listed
	 * @return Boolean
	 * 			true is there are failures listed, false if not
	 * @since v1.0.0
	 */
	public boolean hasFailures() {
		return failures.size() != 0;
	}

	/**
	 * Converts a failure to string
	 * @return String
	 * 			String value describing the failure
	 * @since v1.0.0
	 */
	@SuppressWarnings("rawtypes")
	public String toString() {
		StringBuffer s = new StringBuffer();
		for (Iterator i = failures.iterator(); i.hasNext();) {
			if (s.length() > 0) {
				s.append(System.getProperty("line.separator"));
			}
			s.append(i.next());
		}

		return s.toString();
	}
}
